using System;
using System.Collections.Generic;
using UnityEngine;

namespace Gj
{
    public class EngineService : GameService
    {
        public static EngineService single;
        public static AttrManager AttrManager;
        private Dictionary<int, Buff> buffMap;
        private Dictionary<int, Type> planMap;

        private Dictionary<int, Skill> skillMap;

        static EngineService()
        {
            single = new EngineService();
        }

        public void OnDestroy()
        {
            single = new EngineService();
        }

        public void OnInit()
        {
            skillMap = new Dictionary<int, Skill>();
            buffMap = new Dictionary<int, Buff>();
            planMap = new Dictionary<int, Type>();
        }

        public void OnUpdate()
        {
            
        }

        public void SetSkills(Dictionary<int, Skill> skills)
        {
            skillMap = skills;
        }

        public void SetSkills(object[] arr)
        {
            skillMap.Clear();
            foreach (object[] obj in arr) SetSkill(obj);
        }

        private void SetSkill(object[] obj)
        {
            skillMap.Add(Convert.ToInt32(obj[(byte)SKILL.ID]), new Skill(obj));
            LogTools.Data("Load", "Skill", new Skill(obj).ToString());
        }

        public Skill GetSkill(int id)
        {
            if (skillMap.ContainsKey(id)) return skillMap[id];
            return null;
        }

        public void SetBuffs(Dictionary<int, Buff> buffs)
        {
            buffMap = buffs;
        }

        public void SetBuffs(object[] arr)
        {
            buffMap.Clear();
            foreach (object[] obj in arr) SetBuff(obj);
        }

        private void SetBuff(object[] obj)
        {
            buffMap.Add(Convert.ToInt32(obj[(byte)BUFF.ID]), new Buff(obj, AttrManager));
            LogTools.Data("Load", "Buff", new Buff(obj, AttrManager).ToString());
        }

        public Buff GetBuff(int id)
        {
            if (buffMap.ContainsKey(id)) return buffMap[id];
            return null;
        }

        public void RegisterSkillPlan(int plan, Type type)
        {
            if (planMap.ContainsKey(plan)) return;
            planMap.Add(plan, type);
        }

        private Type GetType(Skill skill)
        {
            if (planMap.ContainsKey(skill.skillPlan)) return planMap[skill.skillPlan];
            return typeof(BaseEngine);
        }

        public BaseEngine MakeEngine(Cast target, string name)
        {
            var baseEngine = ObjectService.single.MakeEngine(name).GetComponent<BaseEngine>();
            baseEngine.Init(target);
            baseEngine.gameObject.SetActive(true);
            return baseEngine;
        }

        public BaseEngine MakeEngine(Cast target, Skill skill)
        {
            var baseEngine = ObjectService.single.MakeEmpty(GetType(skill), "skill" + skill.refInfo.id,
                target.GetPosition(), Quaternion.identity) as BaseEngine;
            baseEngine.Init(target, skill);
            baseEngine.gameObject.SetActive(true);
            return baseEngine;
        }

        public BaseEngine MakeEngine(Cast target, Skill skill, int time)
        {
            var baseEngine = ObjectService.single.MakeEmpty(GetType(skill), "skill" + skill.refInfo.id,
                target.GetPosition(), Quaternion.identity) as BaseEngine;
            baseEngine.Init(target, skill, time);
            baseEngine.gameObject.SetActive(true);
            return baseEngine;
        }

        public void DestroyEngine(GameObject obj)
        {
            obj.SetActive(false);
            ObjectService.single.DestroyObj(obj);
        }
    }
}